package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.action.flow.assign.RegectTaskSingleBackWayCmd;
import com.hnkywl.sts.dao.organ.QyjgDao;
import com.hnkywl.sts.entity.organ.Khh;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.organ.Wls;
import com.hnkywl.sts.service.flow.FlowService;
import com.hnkywl.sts.service.organ.QyjgService;
import com.hnkywl.sts.service.organ.WlsService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.jbpm.api.*;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-22
 * Time: 下午4:30
 * To change this template use File | Settings | File Templates.
 */
@Namespaces({@Namespace("/organ/qyjg"), @Namespace("/organ/lskh")})
public class QyjgAction extends SimpleActionSupport {
    private QyjgService qyjgService;
    private WlsService wlsService;

    private FlowService flowService;

    private ProcessEngine processEngine;

    private TaskService taskService;

    private ExecutionService executionService;

    private HistoryService historyService;

    private Long ljId;

    private Long id;
    //操作类型 add表示新增,update表示修改
    private String operation;
    //企业机构名称
    private String qc;
    private String jc;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //传入的企业机构实体
    private Qyjg qyjg;
    //传入的企业机构Id值
    private Long qyjgId;
    //企业机构和物流商的区别类型
    private Integer type;
    //物流商
    private Wls wls;
    //联系人的姓名
    private String lxr;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;

    private String name_startsWith;

    private Integer maxRows;
    //编码
    private String bm;
    //助记码
    private String zjm;

    private Long nbgsId;

    private String propertyName;

    private String propertyValue;

    private String result;

    private String taskId;

    private String content;

    private String processInstanceId;

    private String executionId;
    private QyjgDao qyjgDao;

    /**
     * 方法名:     获取企业机构列表
     * 功能描述:      获取企业机构列表
     * 输入参数:    <@param qyjg qyjg实例>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("qyjgList")
    public void qyjgList() {
        try {
            if (type != null && type == 0) {
                this.renderText(this.qyjgService.findAllQyjg(qc, jc, lxr, page, rows));
            } else if (type != null && type == 1) {
                this.renderText(this.wlsService.findAllWls(qc, jc, lxr, page, rows));
            } else {
                this.renderText(this.qyjgService.findAllQyjg(qc, jc, lxr, page, rows));
            }
        } catch (Exception e) {
            this.renderJson("");
        }
    }



    @Action("qyjgList1")
    public void qyjgList1() {
        try {
            if (type != null && type == 0) {
                this.renderText(this.qyjgService.findAllQyjg(qc, jc, lxr, page, rows));
            } else if (type != null && type == 1) {
                this.renderText(this.wlsService.findAllWls(qc, jc, lxr, page, rows));
            } else {
                this.renderText(this.qyjgService.findAllQyjg1(qc, jc, lxr, page, rows));
            }
        } catch (Exception e) {
            this.renderJson("");
        }
    }




    /**
     * 查询计划单位
     */
    @Action("jhdwList")
    public void jhdwList() {
        try {
            this.renderJson(this.qyjgService.findAllJhdw(qc, jc, lxr, page, rows));
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    /**
     * 零售客户
     */
    @Action("lskhList")
    public void lskhList() {
        try {
            this.renderJson(this.qyjgService.findAllLskh(qc, jc, page, rows));
        } catch (Exception e) {
            this.renderJson("");
        }
    }


    //企业机构导出
    @Action("qyjg_export")
    public String qyjgExport() {
        try {
            SimpleDateFormat dateformat2 = new SimpleDateFormat("yyyy-MM-dd");
            List<Qyjg> qyjgList = this.qyjgService.findAallQyjg();
            //查找企业机构下有多少开户行
            List<Khh> khhList = this.qyjgService.findKhhByQyjg();
            //第一步，创建一个webbook，对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet("银行明细表");
            POIUtil poiUtil = new POIUtil(wb);
            //设置列宽
            sheet.setDefaultRowHeightInPoints(15);
            sheet.setDefaultColumnWidth((short) 9);
            sheet.setColumnWidth((short) 0, (short) (8 * 512));
            sheet.setColumnWidth((short) 1, (short) (8 * 512));
            sheet.setColumnWidth((short) 2, (short) (8 * 512));
            sheet.setColumnWidth((short) 3, (short) (8 * 512));
            sheet.setColumnWidth((short) 4, (short) (8 * 512));
            sheet.setColumnWidth((short) 5, (short) (8 * 512));
            sheet.setColumnWidth((short) 6, (short) (8 * 512));
            //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            HSSFRow row = sheet.createRow((int) 0);
            row.setHeightInPoints(36);
            //第四步，创建单元格，并设置值表头  设置表头居中
            HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(wb);
            HSSFCell titleCell = row.createCell((short) 0);
            titleCell.setCellStyle(titleStyle);
            titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            titleCell.setCellValue("企业机构下挂开户行汇总表");
            sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 6));
            row = sheet.createRow((int) 1);
            row.setHeightInPoints(21);
            HSSFCell deptCell = row.createCell((short) 1);
            deptCell.setCellStyle(poiUtil.createMonthHSSFCellStyle(wb));
            deptCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            deptCell.setCellValue("导出日期:");
            HSSFCell dqrq = row.createCell((short) 2);
            dqrq.setCellStyle(poiUtil.createMonthHSSFCellStyle(wb));
            dqrq.setEncoding(HSSFCell.ENCODING_UTF_16);
            dqrq.setCellValue(dateformat2.format(new Date()));
            row = sheet.createRow(2);
            row.setHeightInPoints(15);
            HSSFCell khhqc = row.createCell((short) 0);
            khhqc.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            khhqc.setEncoding(HSSFCell.ENCODING_UTF_16);
            khhqc.setCellValue("开户行全称");
            HSSFCell khhjc = row.createCell((short) 1);
            khhjc.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            khhjc.setEncoding(HSSFCell.ENCODING_UTF_16);
            khhjc.setCellValue("开户行简称");
            HSSFCell khhzh = row.createCell((short) 2);
            khhzh.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            khhzh.setEncoding(HSSFCell.ENCODING_UTF_16);
            khhzh.setCellValue("开户行账号");
            HSSFCell zhlx = row.createCell((short) 3);
            zhlx.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            zhlx.setEncoding(HSSFCell.ENCODING_UTF_16);
            zhlx.setCellValue("账户类型");
//            HSSFCell ckhskm = row.createCell((short)4);ckhskm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
//            ckhskm.setEncoding(HSSFCell.ENCODING_UTF_16);
//            ckhskm.setCellValue("存款核算科目");
//            HSSFCell bzjhskm = row.createCell((short)5);bzjhskm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
//            bzjhskm.setEncoding(HSSFCell.ENCODING_UTF_16);
//            bzjhskm.setCellValue("保证金核算科目");
            HSSFCell lxdh = row.createCell((short) 4);
            lxdh.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            lxdh.setEncoding(HSSFCell.ENCODING_UTF_16);
            lxdh.setCellValue("联系电话");
            HSSFCell kmdm = row.createCell((short) 5);
            kmdm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            kmdm.setEncoding(HSSFCell.ENCODING_UTF_16);
            kmdm.setCellValue("科目代码");
            HSSFCell qyjg = row.createCell((short) 6);
            qyjg.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
            qyjg.setEncoding(HSSFCell.ENCODING_UTF_16);
            qyjg.setCellValue("企业机构");
            int z = 0;
            if (qyjgList != null && qyjgList.size() != 0) {
                for (int j = 0; j < qyjgList.size(); j++) {
                    Qyjg q = qyjgList.get(j);
                    if (q.getKhhList() != null && q.getKhhList().size() != 0) {
                        for (int i = 3; i < q.getKhhList().size() + 3; i++) {
                            //表体部分
                            row = sheet.createRow(z + 3);
                            row.setHeightInPoints(15);
                            Khh h = q.getKhhList().get(i - 3);
                            khhqc = row.createCell((short) 0);
                            khhqc.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            khhqc.setEncoding(HSSFCell.ENCODING_UTF_16);
                            khhqc.setCellValue(h.getQc());
                            khhjc = row.createCell((short) 1);
                            khhjc.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            khhjc.setEncoding(HSSFCell.ENCODING_UTF_16);
                            khhjc.setCellValue(h.getJc());
                            khhzh = row.createCell((short) 2);
                            khhzh.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            khhzh.setEncoding(HSSFCell.ENCODING_UTF_16);
                            khhzh.setCellValue(h.getZh());
                            zhlx = row.createCell((short) 3);
                            zhlx.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            zhlx.setEncoding(HSSFCell.ENCODING_UTF_16);
                            zhlx.setCellValue(h.getZhlx() == null ? "" : h.getZhlx().getZ());
//                                ckhskm = row.createCell((short) 4);ckhskm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
//                                ckhskm.setEncoding(HSSFCell.ENCODING_UTF_16);
//                                ckhskm.setCellValue(h.getCkhskm());
//                                bzjhskm = row.createCell((short) 5);bzjhskm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
//                                bzjhskm.setEncoding(HSSFCell.ENCODING_UTF_16);
//                                bzjhskm.setCellValue(h.getBzjhskm());
                            lxdh = row.createCell((short) 4);
                            lxdh.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            lxdh.setEncoding(HSSFCell.ENCODING_UTF_16);
                            lxdh.setCellValue(h.getLxdh());
                            kmdm = row.createCell((short) 5);
                            kmdm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            kmdm.setEncoding(HSSFCell.ENCODING_UTF_16);
                            kmdm.setCellValue(h.getKmdm());
                            qyjg = row.createCell((short) 6);
                            qyjg.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
                            qyjg.setEncoding(HSSFCell.ENCODING_UTF_16);
                            qyjg.setCellValue(q.getQc());
                            z++;
                        }
                    }
                }
            }
            response.setContentType("application/x-download");
            String filedisplay = new String(("企业机构下挂银行账户汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("发生异常，导出失败" + e.getMessage());
        }
        return SUCCESS;
    }

    /**
     * 方法名:     获取企业机构组织树
     * 功能描述:   获取企业机构的组织树
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("qyjg_tree")
    public void qyjgTree() {
        if (type != null && type == 1) {
            this.renderJson(this.wlsService.findAllWlsTree());
        } else {
            this.renderJson(this.qyjgService.findAllQyjgTree());
        }
    }

    @Action("lskh_tree")
    public void lskhTree() {
        this.renderJson(this.qyjgService.findAllLskhTree());
    }

    @Action("lskhjc_tree")
    public void lskhjcTree() {
        this.renderJson(this.qyjgService.findAllLskhJcTree());
    }

    @Action("qyjgjc_tree")
    public void qyjgjcTree() {
        if (type != null && type == 1) {
            this.renderJson(this.wlsService.findAllWlsJcTree());
        } else {
            this.renderJson(this.qyjgService.findAllQyjgJcTree());
        }
    }

    //企业机构的名称显示
    @Action("qyjgmcList")
    public void qyjgmcList() {
        this.renderJson(this.qyjgService.findQyjgMc());
    }


    //查询所有的企业机构简称
    @Action("qyjgjc_list")
    public void qyjgJcList() {
        this.renderJson(this.qyjgService.findQyjgJcNotWls(jc, 1, -1));
    }

    //查询所有的企业机构简称
    @Action("qyjgqc_list")
    public void qyjgQcList() {
        this.renderJson(this.qyjgService.findQyjgQcNotWls(jc, 1, -1));
    }

    //查询所有供应商
    @Action("findAllGysByNbgsId")
    public void findAllGysByNbgsId() {
        this.renderJson(this.qyjgService.findAllGysByNbgsId(nbgsId));
    }

    //查询所有供应商
    @Action("findAllGys")
    public void findAllGys() {
        this.renderJson(this.qyjgService.findAllGys());
    }

    //查询所有库户
    @Action("findAllKhByNbgsId")
    public void findAllKhByNbgsId() {
        this.renderJson(this.qyjgService.findAllKhByNbgsId(nbgsId));
    }

    //查询所有库户
    @Action("findAllKsyJkdByNbgsId")
    public void findAllKsyJkdByNbgsId() {
        this.renderJson(this.qyjgService.findAllKsyJkdByNbgsId(nbgsId));
    }

    //企业机构的零售类型
    @Action("qyjglsList")
    public void qyjglsList() {
        this.renderJson(this.qyjgService.findLxQyjg());
    }

    @Action("qyjgljList")
    public void qyjgljList() {
        this.renderJson(this.qyjgService.findAllLjQyjg());
    }

    //企业机构部门的名称显示
    @Action("qyjgbmMcList")
    public void qyjgbmMcList() {
        this.renderJson(this.qyjgService.findQyjgBm(id));
    }

    @Action("qyjgjhdwMcList")
    public void qyjgjhdwMcList() {
        this.renderJson(this.qyjgService.findJhdwBm(ljId));
    }

    @Action("cghthzfMcList")
    public void cghthzfMcList() {
        this.renderJson(this.qyjgService.findHzfBm(ljId));
    }

    /**
     * 获取所有的联系人
     */
    @Action("qyjgLxrList")
    public void qyjgLxrList() {
        if (type != null && type == 1) {
            this.renderJson(this.wlsService.findWlsLxr());
        } else {
            this.renderJson(this.qyjgService.findQyjgLxr());
        }
    }

    /**
     * 根据助记码或名称选取供应商和客户
     */
    @Action("qyjgLoader")
    public void qyjgLoader() {
        renderJson(qyjgService.findByZjmOrJcOrQc(zjm, jc, qc, id, maxRows));
    }

    /**
     * 查询所有的零售客户
     */
    @Action("lskhLoader")
    public void lskhLoader() {
        renderJson(qyjgService.findLskhByZjmOrJcOrQc(zjm, jc, qc, id, maxRows));
    }

    /**
     * 判断页面的跳转
     *
     * @return
     */
    @Actions({@Action("qyjg_content"), @Action("qyjg_fkfs_content")})
    public String qyjgContent() {
        if ("update".equals(operation)) {
            qyjg = qyjgService.findQyjgDetail(qyjgId);
            if (qyjg instanceof Wls) {
                wls = (Wls) qyjg;
                type = 1;
            } else {
                type = 0;
            }
        } else {
            wls = new Wls();
            qyjg = new Qyjg();
        }
        return SUCCESS;
    }

    /**
     * 判断页面的跳转
     *
     * @return
     */
    @Action("qyjg_detail")
    public String qyjgDetail() {
        qyjg = qyjgService.findQyjgDetail(qyjgId);
        if (qyjg instanceof Wls) {
            wls = (Wls) qyjg;
            type = 1;
        } else {
            type = 0;
        }
        return SUCCESS;
    }


    @Action("lskh_content")
    public String lskhContent() {
        if (qyjgId != null) {
            qyjg = qyjgService.findQyjgDetail(qyjgId);
        } else {
            qyjg = new Qyjg();
        }
        return SUCCESS;
    }


    @Action("qyjgByProcessInstance")
    public void qyjgByProcessInstance() {
        Qyjg qyjg;
        String processInstanceId = StringUtils.EMPTY;
        Execution execution = this.executionService.findExecutionById(executionId);
        if (execution != null) {
            qyjg = this.qyjgService.finQyjgDetailByProcessInstance(execution.getProcessInstance().getId());
            processInstanceId = execution.getProcessInstance().getId();
        } else {
            qyjg = this.qyjgService.finQyjgDetailByProcessInstance(executionId);
            processInstanceId = executionId;
        }
        String xyJson = "{'id':'" + qyjg.getId() + "','type':'QYJG','businessNo':'" + qyjg.getBm() + "','processInstanceId':'" + processInstanceId + "'}";
        this.renderText(xyJson);
    }

    /**
     * 方法名:     增添企业机构
     * 功能描述:      向数据库插入企业机构
     * 输入参数:    <@param qyjg qyjg实例>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("add_qyjg")
    public void addQyjg() {
        try {
            this.qyjgService.saveQyjg(qyjg);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加成功\",\"id\":\"" + qyjg.getId() + "\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("add_lskh")
    public void addLskh() {
        try {
            this.qyjgService.saveLskh(qyjg);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加成功\",\"id\":\"" + qyjg.getId() + "\",\"jc\":\"" + qyjg.getJc() + "\",\"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     删除企业机构
     * 功能描述:      删除企业机构
     * 输入参数:    <@param qyjgId>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */

    @Action("delete_qyjg")
    public void deleteQyjg() {
        try {
            qyjgService.deleteQyjg(qyjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"删除企业机构成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    /**
     * 方法名:     修改企业机构
     * 功能描述:      修改企业机构
     * 输入参数:    <@param qyjg qyjg实例>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     hfj
     * 创建日期:   13-2-19上午10:47
     * 修改人:     hfj
     * 修改日期:   13-2-19上午10:47
     */
    @Action("update_qyjg")
    public void updateQyjg() {
        try {
            qyjgService.updateQyjg(qyjg);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改成功\",\"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 修改零售客户
     */
    @Action("update_lskh")
    public void updateLskh() {
        try {
            qyjgService.updateLskh(qyjg);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改成功\",\"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     start_qyjg
     * 功能描述:      启用企业机构
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("start_qyjg")
    public void startQyjg() {
        try {
            this.qyjgService.startQyjg(qyjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"启用成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     stop_qyjg
     * 功能描述:      停用企业机构
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("stop_qyjg")
    public void stopQyjg() {
        try {
            this.qyjgService.stopQyjg(qyjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"停用成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     qyjg_nbgs
     * 功能描述:      设置企业机构为内部公司
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("qyjg_nbgs")
    public void qyjgNbgs() {
        try {
            this.qyjgService.qyjgNbgs(qyjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"设置成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void validate1() {
        if (!qyjgService.ajaxValidate(qyjgId, validateVal, validateField, type))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    /**
     * 是否选中正确的企业机构
     */
    @Action("ajax_contrary_validate")
    public void contraryValidate() {
        if (qyjgService.ajaxValidate(qyjgId, validateVal, validateField, type))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    /**
     * 获取企业机构所属的内部部门
     */
    @Action("nbjgList")
    public void getNbjg() {
        if (qyjgId != null) {
            this.renderText(this.qyjgService.findNbjg(qyjgId));
        } else if (qyjgId == null) {
            this.renderText("[]");
        }
    }

    /**
     * 方法名:     delete_gsjg
     * 功能描述:      删除公司机构
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("delete_gsjg")
    public void deleteGsjg() {
        try {
            this.qyjgService.deleteNbjg(qyjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"删除内部机构成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     提交协议
     * 功能描述:      提交协议到法务部审核
     * 输入参数:    <@param xyId 协议ID>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Action("commit_qyjg")
    public void commitQyjg() {
        try {
            qyjg = this.qyjgService.findQyjgDetail(qyjgId);
          /*  if (this.flowService.checkFlowStart("企业机构流程")) {
                String processInstanceId = this.flowService.regTask(qyjg.getProcessInstanceId(), "企业机构流程", StringUtils.EMPTY);
                qyjg.setProcessInstanceId(processInstanceId);
                qyjg.setZt("ytj");
            } else*/
              //  qyjg.setZt("ysp");
          //  this.qyjgService.updateQyjg(qyjg);
            this.qyjgService.commitqyjg(qyjg);

            renderText("{\"statusCode\":\"200\", \"message\":\"提交企业机构成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action(value = "examineTask")
    public void examineTask() {
        if (this.result.equals("审核")) {
            Map<String, Object> var = new HashMap<String, Object>();
            var.put("owner", YgService.getCurrentYg().getId());
            var.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
            var.put("result", "审核通过");
            var.put("content", content);
            var.put("time", new Date());
            Task currentTask = this.taskService.getTask(taskId);
            String executionId = currentTask.getExecutionId();
            executionService.createVariables(executionId, var, true);
            var.put("status", result);
            this.taskService.completeTask(taskId, var);

            Execution execution = this.executionService.findExecutionById(executionId);
            if (execution != null) {
                Qyjg qyjg = qyjgService.finQyjgDetailByProcessInstance(execution.getProcessInstance().getId());
                if (qyjg.getZt().equals("ybc")) {
                    qyjg.setZt("ytj");
                    qyjgService.updateQyjg(qyjg);
                }
            }
        } else {
            String result = processEngine.execute(new RegectTaskSingleBackWayCmd(taskId, content));
            if (result.equals("change")) {
                //修改状态  ytj--->ybc
                String executionId = this.historyService.createHistoryTaskQuery().taskId(taskId).uniqueResult().getExecutionId();
                Execution execution = this.executionService.findExecutionById(executionId);
                Qyjg qyjg = qyjgService.finQyjgDetailByProcessInstance(execution.getProcessInstance().getId());
                qyjg.setZt("ybc");
                qyjgService.updateQyjg(qyjg);
            }
        }
        this.renderText("{\"statusCode\":\"200\", \"message\":\"审核流程成功\"}");
    }

    @Action("lskhCheckProperty")
    public void lskhCheckProperty() {
        this.renderJson(this.qyjgService.lskhCheckProperty(propertyName, propertyValue));
    }

    @Action("findAllSy")
    public void findAllSy() {
        renderJson(qyjgService.findAllSy(name_startsWith, maxRows));
    }

    @Action("findAllXy")
    public void findAllXy() {
        renderJson(qyjgService.findAllXy(name_startsWith, maxRows));
    }


    @Action("qyjgfkfsList")
    public void qyjgfkfsList() {
        renderJson(qyjgService.findFkfsByQyjgId(qyjgId));
    }

    @Action("qyjg_fkfs")
    public void qyjgFkfs() {
        try {
            this.qyjgService.qyjgFkfs(qyjg);
            renderText("{\"statusCode\":\"200\", \"message\":\"调整成功！\",\"id\":\"" + qyjg.getId() + "\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    @Action("findEjzhKhId")
    public void findQyjgList() {
        this.qyjgDao.findQyjgList(qyjgId);
    }

    //============getter setter============


    public Long getLjId() {
        return ljId;
    }

    public void setLjId(Long ljId) {
        this.ljId = ljId;
    }

    public String getJc() {
        return jc;
    }

    public void setJc(String jc) {
        this.jc = jc;
    }

    public String getName_startsWith() {
        return name_startsWith;
    }

    public void setName_startsWith(String name_startsWith) {
        this.name_startsWith = name_startsWith;
    }

    public Integer getMaxRows() {
        return maxRows;
    }

    public void setMaxRows(Integer maxRows) {
        this.maxRows = maxRows;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public Long getQyjgId() {
        return qyjgId;
    }

    public void setQyjgId(Long qyjgId) {
        this.qyjgId = qyjgId;
    }

    public String getQc() {
        return qc;
    }

    public void setQc(String qc) {
        this.qc = qc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public String getLxr() {
        return lxr;
    }

    public void setLxr(String lxr) {
        this.lxr = lxr;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Qyjg getQyjg() {
        return qyjg;
    }

    public void setQyjg(Qyjg qyjg) {
        this.qyjg = qyjg;
    }

    public Wls getWls() {
        return wls;
    }

    public void setWls(Wls wls) {
        this.wls = wls;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public String getBm() {
        return bm;
    }

    public void setBm(String bm) {
        this.bm = bm;
    }

    public String getZjm() {
        return zjm;
    }

    public void setZjm(String zjm) {
        this.zjm = zjm;
    }

    public Long getNbgsId() {
        return nbgsId;
    }

    public void setNbgsId(Long nbgsId) {
        this.nbgsId = nbgsId;
    }

    public String getPropertyValue() {
        return propertyValue;
    }

    public void setPropertyValue(String propertyValue) {
        this.propertyValue = propertyValue;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getProcessInstanceId() {
        return processInstanceId;
    }

    public void setProcessInstanceId(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    public String getExecutionId() {
        return executionId;
    }

    public void setExecutionId(String executionId) {
        this.executionId = executionId;
    }

    @Autowired
    public void setQyjgService(QyjgService qyjgService) {
        this.qyjgService = qyjgService;
    }

    @Autowired
    public void setWlsService(WlsService wlsService) {
        this.wlsService = wlsService;
    }

    @Autowired
    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }

    @Autowired
    public void setExecutionService(@Qualifier("executionService") ExecutionService executionService) {
        this.executionService = executionService;
    }

    @Autowired
    public void setTaskService(@Qualifier("taskService") TaskService taskService) {
        this.taskService = taskService;
    }

    @Autowired
    public void setProcessEngine(@Qualifier("processEngine") ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }


    public void setHistoryService(@Qualifier("historyService") HistoryService historyService) {
        this.historyService = historyService;
    }
    @Autowired
    public void setQyjgDao(QyjgDao qyjgDao) {
        this.qyjgDao = qyjgDao;
    }
}
